//  Copyright (c) Whitemice Consulting, 2007, 2008, 2009
//  Adam Tauno Williams <awilliam@whitemice.org>
//  License: MIT/X11
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Gtk;

namespace Whitemice.ZOGI.GtkWidgets
{
  public class HistoryTab : Gtk.Bin, IEntityForm
  {
    public EventHandler             OnDataReceived;
    protected MultiResultCallback   callback;
    protected int                   currentObjectId;
    
    public HistoryTab() : base()
    {
      currentObjectId = 0;
      callback = new MultiResultCallback(this.ReceiveDataCallback);
      this.Child = new TaskHistoryForm();
      ShowAll();
    }

    public void Clear()
    {
      (Child as TaskHistoryForm). Clear();
    } // end Clear

    public void Fill(Entity entity)
    {
      this.ZLOG("Fill");
      if (entity is Whitemice.ZOGI.Task)
      {
        // we keep the objectId of the last object poured in the form
        // so that we can verify the identify of subsequently received
        // data from the async. server call
        currentObjectId = entity.ObjectId;
      } else throw new Exception("Inappopriate entity type provided to form");
      this.Sensitive = false;
      this.ZLOG("Issuing fill request.");
      var command = new FetchCommand(currentObjectId, 
                                     RpcPriority.Application, 
                                     null, 
                                     65535, 
                                     currentObjectId);
      command.Callback = callback;
      Server.Connection.RunCommand(command);
    } // end Fill

    public void ReceiveRPCCall(RPCEventArgs args)
    {
      this.Sensitive = true;
      this.ZLOG("ReceiveRPCCall");
      if (args.IsError)
      {
        // TODO: Raise error message box for HistoryTab RPC exception
        this.ZLOG(args.Exception.Message);
        this.ZLOG(args.Exception.StackTrace);
      } else
        {
          // We discard received data if the context is not the objectId of the
          // currently displayed enterprise.  This is because scrolling around
          // in a CRUD form the data here may lag that displayed in the detail
          // section and we do not want to display incorrect information.  The
          // correct data should be received eventually. :)
          if (Convert.ToInt32(args.Context) == currentObjectId)
          {
            this.ZLOG("Filling task history tab");
            var task = (args.Payload.ElementAt(0) as Whitemice.ZOGI.Task);
            (Child as TaskHistoryForm).Fill(task);
          } else this.ZLOG("Discarding received annotations due to objectId mismatch", args.Payload.Count());
        }
      if (OnDataReceived != null)
        OnDataReceived(this, null);
    } // end FillCallback
  }
}
